<%@ include file="../import_page.jsp"%>
<%
	//login checking
	if(!loginedChecking(session, UsersType.student)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	//intput parament
	int acId = -1;
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	try {
		if(isEmptyString(acIdStr))
			throw new Exception("No parament input");
		acId = Integer.parseInt(acIdStr);
		if(acId < 1)
			throw new Exception("Incorrect assignment ID");
	} catch (Exception err) {
		//if fail to get parament, go back
		response.sendRedirect(URL_SELECT_ASSIGNMENT);
		return;
	}
	
	Users loginUser = (Users)session.getAttribute(SESSION_LOGIN_USER);
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
	String errMsg = null;
	
	AssignmentCollector ac = null;
	//get assignment, detail result
	try {
		AssignmentsControl assCon = new AssignmentsControl();
		
		ac = assCon.getAssignment(acId);
		
		assCon.closeDBConnection();
		
	} catch (Exception err) {
		errMsg = err.toString();
	}
	
	//Check if the assignment is expired, return to view assignment page
	if(ac.isExpired()) {
		response.sendRedirect(URL_SELECT_ASSIGNMENT);
		return;
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload Assignment</title>
</head>
<body>
	<%if(isEmptyString(errMsg)) { %>
	<form
		action="<%= addParmeterToUrl(URL_CONFIRM_SUBMISSION, PAR_ASSIGNMENT_ID, Integer.toString(ac.getId())) %>"
		method="post" enctype="multipart/form-data">

		<table width="100%" border="1">
			<tr>
				<td colspan="2" bgcolor="#CCFF00"><p>
						Unit:
						<%=selectUnit.getName()  %></p>
				</td>
			</tr>
			<tr>
				<td>Student Name (ID)</td>
				<td><%= loginUser.getName() %> (<%=loginUser.getId() %>)</td>
			</tr>
			<tr>
				<td>Assignment Name</td>
				<td><%= ac.getName() %></td>
			</tr>
			<tr>
				<td>Assignment Type</td>
				<td><%= (ac.getGroupSize() < 2 ? "Individual" : ("Group (" + ac.getGroupSize()+ ")")) %></td>
			</tr>
			<tr>
				<td>Upload Assignment (Maximum File Size: 4MB)</td>
				<td><input type="file" name="<%=PAR_UPLOAD_FILE %>"
					id="<%=PAR_UPLOAD_FILE %>" />
				</td>
			</tr>
			<tr>
				<td colspan="2"><p>
						<input type="submit" value="Submit Now" />
					</p>
				</td>
			</tr>
		</table>
	</form>
	<%} else { %>
	<p><%= errMsg %></p>
	<% } %>
</body>
</html>